package beanAction;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.FacesContext;

import modele.beanEntity.Recipe;
import modele.beanEntity.Comment;
import modele.beanEntity.User;
import modele.mapper.CommentMapper;

@ManagedBean(name="commentManager")
public class CommentManagerBean {
	@ManagedProperty(value="#{searchRecipe}")
	private SearchRecipeBean searchRecipeBean;
	
	private Comment newComment = new Comment(); 

	public CommentManagerBean() {}
	
	public void saveComment() {
		CommentMapper cm = null;
		
		try {
			cm = new CommentMapper();
		} catch (Exception e) {
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Impossible d'itinialiser la connexion avec la base de données dans le mapper :\n" + e.getMessage(), null));
		}
		
		try {
			cm.createComment(newComment);
			searchRecipeBean.refreshComments();
			newComment = new Comment();
		} catch (SQLException e) {
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Impossible de créer ton commentaire :\n" + e.getMessage(), null));
		}
	}

	public Comment getNewComment() {
		return newComment;
	}

	public void setNewComment(Comment newComment) {
		this.newComment = newComment;
	}

	public SearchRecipeBean getSearchRecipeBean() {
		return searchRecipeBean;
	}

	public void setSearchRecipeBean(SearchRecipeBean searchRecipeBean) {
		this.searchRecipeBean = searchRecipeBean;
	}
}
